package de.tabuma.pm.gadget.controller;

import de.tabuma.pm.*;
import de.tabuma.pm.gadget.entity.*;
import de.tabuma.pm.gadget.logik.*;
import de.tabuma.pm.externalproject.entity.*;
import de.tabuma.pm.externalproject.logik.*;
import de.tabuma.pm.htmlcontent.entity.*;
import de.tabuma.pm.htmlcontent.logik.*;
import de.tabuma.pm.externalcontent.entity.*;
import de.tabuma.pm.externalcontent.logik.*;
import de.tabuma.lib.tabumacontroller;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.util.logging.*;
import com.google.appengine.api.users.*;
import com.google.appengine.api.datastore.Text;

/**
 *
 * @author alefen
 */
public class settings extends tabumacontroller {

    private static final Logger log = Logger.getLogger(settings.class.getName());

    public settings(String servlet, Hashtable hashtable) {
        super(servlet, hashtable);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        if(request.getParameter(constants.cancel)!=null) {
            list list = new list(this.getServlet(), this.getParameter());
            list.navigate(context, request, response, out);
            return;
        }
        request.setAttribute(constants.gadgetActionId, "" + constants.GADGET_ACTION_SETTING);
        request.setAttribute(constants.GADGET_ACTION_SETTING_ACTION, "" +  request.getParameter(constants.GADGET_ACTION_SETTING_ACTION));
        String gadgetId = request.getParameter(constants.gadgetId);
        int gadgetIdInt = Integer.valueOf(gadgetId).intValue();
        Gadget gadget = GadgetFacade.get(gadgetIdInt);
        request.setAttribute(constants.gadget, gadget);
        
        String projectId = request.getParameter(constants.projectId);
        int projectIdInt = Integer.valueOf(projectId).intValue();
        
        List<ExternalProject> eList = ExternalProjectFacade.getList(projectIdInt);

        request.setAttribute(constants.externalProjectList, eList);
        
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        log.warning(constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT_UPDATAE+":"+request.getParameter(constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT_UPDATAE));
        if(request.getParameter(constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT_UPDATAE)!=null) {
            removeContent(gadget);
            String externalProjectId = request.getParameter(constants.externalProjectId);
            int externalProjectIdInt = Integer.valueOf(externalProjectId).intValue();
            String minutes = request.getParameter(constants.GADGET_MINUTES);
            int minutesInt = Integer.valueOf(minutes);
            String url = request.getParameter(constants.externalContentUrl);
            ExternalContent externalContent = new ExternalContent();
            externalContent.setExternProjectId(externalProjectIdInt);
            externalContent.setUrl(url);
            externalContent.setNavigationId(0);
            int id = ExternalContentFacade.create(externalContent, user.getEmail());
            gadget.setNavMode(constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT);
            gadget.setNavModeElementId(id);
            gadget.setUpdateTime(minutesInt);
            GadgetFacade.update(gadget, user.getEmail());
            list list = new list(this.getServlet(),this.getParameter());
            list.navigate(context, request, response, out);
            return;
        }
        log.warning(constants.GADGET_ACTION_SETTING_HTML_CONTENT_UPDATE+":"+request.getParameter(constants.GADGET_ACTION_SETTING_HTML_CONTENT_UPDATE));
        if(request.getParameter(constants.GADGET_ACTION_SETTING_HTML_CONTENT_UPDATE)!=null) {
            removeContent(gadget);
            HtmlContent htmlContent = new HtmlContent();
            String htmlContent1 = request.getParameter(constants.HtmlContentTextareaName);
            htmlContent.setText(new Text(htmlContent1));
            htmlContent.setProjectnavigationId(0);
            int id = HtmlContentFacade.create(htmlContent, user.getEmail());
            gadget.setNavMode(constants.GADGET_ACTION_SETTING_HTML_CONTENT);
            gadget.setNavModeElementId(id);
            gadget.setUpdateTime(0);
            GadgetFacade.update(gadget, user.getEmail());
            list list = new list(this.getServlet(),this.getParameter());
            list.navigate(context, request, response, out);
            return;
        }
        switch(gadget.getNavMode()) {
            case constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT:
                ExternalContent externalContent = ExternalContentFacade.get(gadget.getNavModeElementId());
                request.setAttribute(constants.externalContentUrl, externalContent.getUrl());
                request.setAttribute(constants.externalProjectId, ""+externalContent.getExternProjectId());
                break;
            default:
                HtmlContent htmlContent = HtmlContentFacade.get(gadget.getNavModeElementId());
                request.setAttribute(constants.HtmlContentTextareaName, htmlContent.getText());
                break;
        }
    }
    public static void removeContent(Gadget gadget) {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        switch(gadget.getNavMode()) {
            case constants.GADGET_ACTION_SETTING_EXTERNAL_CONTENT:
                ExternalContent externalContent = ExternalContentFacade.get(gadget.getNavModeElementId());
                if(externalContent!=null) {
                    ExternalContentFacade.remove(externalContent.getId().intValue(), user.getEmail());
                }
                break;
            default:
                HtmlContent htmlContent = HtmlContentFacade.get(gadget.getNavModeElementId());
                if(htmlContent!=null) {
                    HtmlContentFacade.remove(htmlContent.getId().intValue(), user.getEmail());
                }
                break;
        }
    }
}